package com.base.model;

import com.base.BaseJsonUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.*;

/**
 * @author gaoxing
 * @create 2018-09-17 下午2:46
 */
@Data
public class DirInfo {
    private String relativePath;
    private Map<String, DirInfo> childDirs = new HashMap(0);
    private List<File> files = new ArrayList(0);

    public DirInfo() {
    }

    public DirInfo(String relativePath) {
        this.relativePath = relativePath;
    }

    public DirInfo createPath(String path) {
        path = StringUtils.isNoneBlank(new CharSequence[]{path}) ? path.replaceFirst("^" + File.separator, "") : path;
        path = StringUtils.isNoneBlank(new CharSequence[]{path}) ? path.replaceFirst(File.separator + "$", "") : path;
        if (StringUtils.isBlank(path)) {
            return this;
        } else {
            int index = path.indexOf(File.separator);
            DirInfo dirInfo;
            if (index == -1) {
                if (!this.childDirs.containsKey(path)) {
                    this.getChildDirs().put(path, new DirInfo(this.relativePath + File.separator + path));
                }

                dirInfo = (DirInfo)this.getChildDirs().get(path);
            } else {
                String childPath = path.substring(0, index);
                if (!this.childDirs.containsKey(childPath)) {
                    this.getChildDirs().put(childPath, new DirInfo(this.relativePath + File.separator + childPath));
                }

                dirInfo = ((DirInfo)this.getChildDirs().get(childPath)).createPath(path.substring(index + File.separator.length()));
            }

            return dirInfo;
        }
    }

    public void createFile(String path, File file) {
        if (!StringUtils.isBlank(path) && !path.equals(File.separator)) {
            DirInfo dirInfo = this.createPath(path);
            dirInfo.getFiles().add(file);
        } else {
            this.getFiles().add(file);
        }

    }

    public Set<String> getDirs(String path) {
        DirInfo dirInfo = this.getDirInfo(path);
        return dirInfo != null ? dirInfo.getChildDirs().keySet() : Collections.EMPTY_SET;
    }

    public List<File> getFiles(String path) {
        DirInfo dirInfo = this.getDirInfo(path);
        return dirInfo != null ? dirInfo.getFiles() : Collections.emptyList();
    }

    @Override
    public String toString() {
        return BaseJsonUtils.writeValue(this);
    }

    private DirInfo getDirInfo(String path) {
        path = StringUtils.isNoneBlank(new CharSequence[]{path}) ? path.replaceFirst("^" + File.separator, "") : path;
        path = StringUtils.isNoneBlank(new CharSequence[]{path}) ? path.replaceFirst(File.separator + "$", "") : path;
        DirInfo dirInfo = this;
        if (StringUtils.isNotBlank(path)) {
            String fileSep = File.separator.equals("\\") ? "\\\\" : File.separator;
            String[] var4 = path.split(fileSep);
            int var5 = var4.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String crtPath = var4[var6];
                dirInfo = (DirInfo)dirInfo.getChildDirs().get(crtPath);
            }
        }

        return dirInfo;
    }}
